﻿using Cloud.BLL;
using Cloud.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// FSDXCommon 的摘要说明
/// </summary>
public class FSDXCommon
{
    private const MySqlConnection conn = null;
    public FSDXCommon()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public bool InsertFSDX(string Telephone, string Content)
    {
        bool result = false;
        try
        {
            string ConnectionStrings = @"Data Source=223.100.97.174;database=dbadapter;uid=root;pwd=tongze@2011;port=20123";
            MySqlConnection conn = new MySqlConnection(ConnectionStrings);
            conn.Open();
            string sql = "insert into sms_outbox(sismsid,extcode,destaddr,messagecontent,reqdeliveryreport,msgfmt,sendmethod,requesttime,applicationid) values ('64a24682-e267-4564-8e58-6450d74e631e','300','" + Telephone + "','" + Content + "',1,15,0,'" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "','300')";
            using (MySqlTransaction transaction = conn.BeginTransaction())
            {
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    try
                    {
                        cmd.Transaction = transaction;
                        cmd.CommandText = sql;
                        int count = cmd.ExecuteNonQuery();
                        
                        Sms_outbox sms_box = new Sms_outbox();
                        sms_box.Sismsid = "64a24682-e267-4564-8e58-6450d74e631e";
                        sms_box.Extcode = "300";
                        sms_box.Destaddr = Telephone;
                        sms_box.Messagecontent = Content;
                        sms_box.Reqdeliveryreport = 1;
                        sms_box.Msgfmt = 15;
                        sms_box.Sendmethod = 0;
                        sms_box.Requesttime = DateTime.Now;
                        sms_box.Applicationid = "300";
                        int smsSave = Sms_outboxManager.Save(sms_box);
                        transaction.Commit();
                        if (count > 0 && smsSave>0)
                        {
                            result = true;
                        }
                        else
                        {
                            transaction.Rollback(); 
                            result = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback(); //事务回滚
                        Console.Write("<script>alert('短信发送失败');</script>");
                    }
                }
                
            }

        }
        catch (Exception ex)
        {
            Console.Write("错误信息:" + ex.Message);
        }
        finally
        {
            conn.Close();
        }
        return result;
    }
}